package com.gtw.modules.sys.controller;

import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.entity.TbProxyFavorable;
import com.gtw.modules.sys.service.TbProxyFavorableService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/** 代理进货优惠Controller
 * Created by wuye on 2018/3/29.
 */
@RestController
@RequestMapping("/sys/proxyfavorable")
public class TbProxyFavorableController extends AbstractController{

    @Autowired
    private TbProxyFavorableService proxyFavorableService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:proxyfavorable:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<TbProxyFavorable> proxyFavorableList = proxyFavorableService.queryList(query);
        int total = proxyFavorableService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(proxyFavorableList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:proxyfavorable:info")
    public R info(@PathVariable("id") Integer id){
        TbProxyFavorable proxyFavorable = proxyFavorableService.queryObject(id);
        proxyFavorable.setProportion(proxyFavorable.getProportion()*100);
        proxyFavorable.setMaxprice(proxyFavorable.getMaxprice());
        proxyFavorable.setMinprice(proxyFavorable.getMinprice());
        return R.ok().put("proxyFavorable", proxyFavorable);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:proxyfavorable:save")
    public R save(@RequestBody TbProxyFavorable proxyFavorable){
        proxyFavorable.setCreatetime(new Date());
        proxyFavorable.setCreateuser(getUser().getUsername());
        proxyFavorable.setMinprice(proxyFavorable.getMinprice());
        proxyFavorable.setMaxprice(proxyFavorable.getMaxprice());
        proxyFavorable.setProportion(proxyFavorable.getProportion()/100);
        proxyFavorableService.save(proxyFavorable);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:proxyfavorable:update")
    public R update(@RequestBody TbProxyFavorable proxyFavorable){
        proxyFavorable.setMinprice(proxyFavorable.getMinprice());
        proxyFavorable.setMaxprice(proxyFavorable.getMaxprice());
        proxyFavorable.setProportion(proxyFavorable.getProportion()/100);
        proxyFavorableService.update(proxyFavorable);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:proxyfavorable:delete")
    public R delete(@RequestBody Integer[] ids){
        proxyFavorableService.deleteBatch(ids);

        return R.ok();
    }
}
